package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginSysUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    //r
    @Autowired
    private RoomLabelMapper rlabelMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;


    //apa
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Override
    public BigDecimal minRentByApartmentId(Long id) {
        return roomInfoMapper.minRentByApartmentId(id);
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {

        IPage<RoomItemVo>   roomItemVoIPage = roomInfoMapper.getPageOfRoom(page, queryVo);
        List<RoomItemVo> records = roomItemVoIPage.getRecords();
        records.forEach(r->{
            List<LabelInfo> labelInfos = labelInfoMapper.selectListById(r.getId());
            System.out.println(labelInfos);

            List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, r.getId())
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getIsDeleted, 0));
            List<GraphVo> graphVoList = new ArrayList<>();
                graphInfos.forEach(g->{
                GraphVo graphVo = GraphVo.builder()
                .name(g.getName())
                .url(g.getUrl())
                .build();
                graphVoList.add(graphVo);});
            r.setGraphVoList(graphVoList);
        });
        roomItemVoIPage.setRecords(records);

        //查询RoomItemVo 需要获取房间信息 图片信息 标签信息 房间所属公寓信息
        //先将所有数据进行分页查询 如何再加入RoomItemVo返回数据


        return roomItemVoIPage;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        RoomDetailVo detailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,detailVo);
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, roomInfo.getApartmentId())
                .eq(ApartmentInfo::getIsDeleted, 0));
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        List<LabelInfo> apalabelInfos = apartmentLabelMapper.selectOneByApartmentId(apartmentInfo.getId());
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, apartmentInfo.getId()).eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getIsDeleted, 0));
        apartmentItemVo.setGraphVoList(graphInfos);
        apartmentItemVo.setLabelInfoList(apalabelInfos);
        apartmentItemVo.setMinRent(this.minRentByApartmentId(apartmentInfo.getId()));
        detailVo.setApartmentItemVo(apartmentItemVo);

        List<GraphInfo> graphInfolist = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomInfo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getIsDeleted, 0));
        List<GraphVo> graphVoList = new ArrayList<>();
        graphInfolist.forEach(g->{
            GraphVo graphVo = GraphVo.builder().url(g.getUrl()).name(g.getName()).build();
            graphVoList.add(graphVo);
        });
        detailVo.setGraphVoList(graphVoList);

        List<AttrValueVo> valueVos = roomAttrValueMapper.getValueByRoomId(id);
        detailVo.setAttrValueVoList(valueVos);

        List<FacilityInfo> facilityInfos = roomFacilityMapper.getfacilityByRoomId(id);
        detailVo.setFacilityInfoList(facilityInfos);

        List<LabelInfo> labelInfos = labelInfoMapper.selectListById(id);
        detailVo.setLabelInfoList(labelInfos);

        List<PaymentType> paymentTypes = roomPaymentMapper.getPaymentByRoomId(id);
        detailVo.setPaymentTypeList(paymentTypes);

        List<FeeValueVo> feeValueVos = apartmentFeeValueMapper.getFeeByRoomId(roomInfo.getApartmentId());
        detailVo.setFeeValueVoList(feeValueVos);

        List<LeaseTerm> leaseTerms = roomLeaseTermMapper.getLeaseTermByRoomId(id);
        detailVo.setLeaseTermList(leaseTerms);

        Boolean isCheckIn = leaseAgreementMapper.getCheckInByroomId(id);
        detailVo.setIsCheckIn(isCheckIn);
        return detailVo;
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemVoByApartmentId(Page<RoomItemVo> page, Long id) {
        IPage<RoomItemVo>   roomItemVoIPage = roomInfoMapper.pageRoomItemVoByApartmentId(page, id);
        List<RoomItemVo> records = roomItemVoIPage.getRecords();
        records.forEach(r->{
            List<LabelInfo> labelInfos = labelInfoMapper.selectListById(r.getId());
            System.out.println(labelInfos);

            List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, r.getId())
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getIsDeleted, 0));
            List<GraphVo> graphVoList = new ArrayList<>();
            graphInfos.forEach(g->{
                GraphVo graphVo = GraphVo.builder()
                        .name(g.getName())
                        .url(g.getUrl())
                        .build();
                graphVoList.add(graphVo);});
            r.setGraphVoList(graphVoList);
        });
        roomItemVoIPage.setRecords(records);

        //查询RoomItemVo 需要获取房间信息 图片信息 标签信息 房间所属公寓信息
        //先将所有数据进行分页查询 如何再加入RoomItemVo返回数据


        return roomItemVoIPage;
    }
}




